#!/bin/bash

#
# 脚本用途
# 将 xxl-job-admin 创建为 spring-boot-starter
#


# 复制 xxl-job-admin 源码
cp -R xxl-job/xxl-job-admin/src xxl-job-spring-boot-starter-server
cp -R xxl-job/xxl-job-admin/pom.xml xxl-job-spring-boot-starter-server/pom.xml





# 删除启动类
rm xxl-job-spring-boot-starter-server/src/main/java/com/xxl/job/admin/XxlJobAdminApplication.java





# 准备默认配置文件
sed '{
/^server.port/d
/^server.servlet.context-path/d
/^spring.mail/d
}' xxl-job/xxl-job-admin/src/main/resources/application.properties > xxl-job-spring-boot-starter-server/src/main/resources/xxl-job-server-default.properties

rm xxl-job-spring-boot-starter-server/src/main/resources/application.properties
rm xxl-job-spring-boot-starter-server/src/main/resources/logback.xml




# 处理 EmailJobAlarm.java
sed -i '/^@Component/d' xxl-job-spring-boot-starter-server/src/main/java/com/xxl/job/admin/core/alarm/impl/EmailJobAlarm.java





# 注解类 EnableXxlJobServer.java , 用于注解启用 XxlServer
cp EnableXxlJobServer.java xxl-job-spring-boot-starter-server/src/main/java/com/xxl/job/admin/





# 配置 pom.xml

sed -i '{
s/xxl-job-admin/xxl-job-spring-boot-starter-server/
}' xxl-job-spring-boot-starter-server/pom.xml

begin_line=$( sed -n '/<build/=' xxl-job-spring-boot-starter-server/pom.xml )
end_line=$( sed -n '/<\/build>/=' xxl-job-spring-boot-starter-server/pom.xml )
sed -i "${begin_line},${end_line}d" xxl-job-spring-boot-starter-server/pom.xml





# 清理新增的文件
echo  "代码生成完毕, 命令行到 xxl-job-spring-boot-starter-server 目录下, 执行 maven 命令构建. "
echo "比如: "
echo " mvn clean compile package -DskipTests "
echo " mvn clean compile install -DskipTests "
echo " mvn clean compile deploy -DskipTests "

echo  "构建之后, 清理 xxl-job-spring-boot-starter-server 目录 (删除自动生成的文件与文件夹)"
echo " 执行 git clean -xdf "
